﻿/***********************************************************************/
/* Crytography class                                                   */
/* Created          : Tan Chun Mun                                     */
/* Date             : 29 Jan 2008                                      */
/* Copyrighted      :                                                  */
/*****************************Description*******************************/
/*        This class is created to allow singleton created object      */
/*****************************Revision**********************************/
/*                                                                     */
/***********************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Singleton
{
    public sealed class Singleton <T> where T : class
    {
       
        //To avoid user from creating object
        private Singleton()
        {

        }
        #region Internal class
        class SingletonCreator
        {
            private static LinkedList<object> list = new LinkedList<object>();
            //avoid user from init the class
            private SingletonCreator()
            {
                
            } 
            //redundant as it is created by the compiler automatically
            
            private static readonly T _instance = typeof(T).InvokeMember(typeof(T).Name,
                                 BindingFlags.CreateInstance |
                                 BindingFlags.Instance |
                                 BindingFlags.NonPublic |
                                 BindingFlags.Public,
                                 null, null, null) as T;
            
            public static T CreatedInstance
            {
                get 
                {
                    return _instance; 
                }
            }
        }

        #endregion
        #region Properties
        public static T Instance
        {
            get
            {
                return SingletonCreator.CreatedInstance;
            }
        }
        #endregion
 
    }
   
}
